package com.zpc.myvideoserver.common.config;

import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.persistent.FileRecorder;
import com.qiniu.util.Auth;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.nio.file.Paths;

/**
 * @ClassName : QiniuConfig  //类名
 * @Description : 七牛云配置类  //描述
 * @Author : zpc20 //作者
 * @Date: 2023/11/6  8:31
 */
@Configuration
public class QiniuConfig {
    @Value("${qiNiu.accessKey}")
    private String accessKey;
    @Value("${qiNiu.secretKey}")
    private String secretKey;
    @Value("${qiNiu.bucketName}")
    private String bucketName;
    @Value("${qiNiu.domain}")
    private String domain;
       @Bean
       public BucketManager bucketManager(){
           Auth auth = Auth.create(accessKey, secretKey);
           com.qiniu.storage.Configuration cfg = new com.qiniu.storage.Configuration(Region.huanan());
           return new BucketManager(auth, cfg);
       }
       @Bean
       public Auth auth(){
           return Auth.create(accessKey, secretKey);
       }

       @Bean
       public UploadManager uploadManager() throws IOException {
           //设置断点续传文件进度保存目录
           String localTempDir = Paths.get(System.getenv("java.io.tmpdir"), bucketName).toString();
           FileRecorder fileRecorder = new FileRecorder(localTempDir);
           com.qiniu.storage.Configuration cfg = new com.qiniu.storage.Configuration(Region.huanan());
           cfg.resumableUploadAPIVersion = com.qiniu.storage.Configuration.ResumableUploadAPIVersion.V2;
           cfg.resumableUploadMaxConcurrentTaskCount = 2;
           return new UploadManager(cfg, fileRecorder);
       }

}
